package com.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class QRcodeUtil {
	public static String generateQRcode(String matId) {
		String path = "";
		try {			
//			path = ServletActionContext.getRequest().getRealPath("/") + "\\QRcode";
			path = System.getProperty("user.dir")+"/WebRoot/QRcode";
			File f = new File(path);
			if (!f.exists()) {
				f.mkdirs();
			}
			path += "\\" + matId + ".jpg";
			MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
			Map hints = new HashMap();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			BitMatrix bitMatrix = multiFormatWriter.encode(matId, BarcodeFormat.QR_CODE, 1000, 1000, hints);
			File file1 = new File(path);
			MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path;
	}
	
	public static String generateQRcode(String path,String fileName,String value) {
		try {			
			File f = new File(path);
			if (!f.exists()) {
				f.mkdirs();
			}
			path += "\\" + fileName;
			MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
			Map hints = new HashMap();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			BitMatrix bitMatrix = multiFormatWriter.encode(value, BarcodeFormat.QR_CODE, 1000, 1000, hints);
			File file1 = new File(path);
			MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path;
	}

}
